package com.hh.controller;

import com.hh.common.BaseResponse;
import com.hh.common.ErrorCode;
import com.hh.common.ResultUtils;
import com.hh.entity.domain.Message;
import com.hh.entity.domain.User;
import com.hh.entity.dto.follow.DoFollowRequest;
import com.hh.entity.enums.MessageType;
import com.hh.entity.vo.UserVO;
import com.hh.exception.BusinessException;
import com.hh.exception.ThrowUtils;
import com.hh.service.FollowService;
import com.hh.service.MessageService;
import com.hh.service.UserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * com.hh.controller
 *
 * @author hanhui
 * @version 1.0
 * @date 2025/8/2 17:00
 **/
@RestController
@Slf4j
@RequestMapping("/follow")
@RequiredArgsConstructor
public class FollowController {

    private final FollowService followService;
    private final UserService userService;
    private final MessageService messageService;

    /**
     * 用户关注
     *
     * @param doFollowRequest 用户ID
     * @param request         HttpServletRequest对象
     * @return BaseResponse<Boolean> 返回操作结果
     */
    @PostMapping("/followUser")
    public BaseResponse<Boolean> followUser(@RequestBody DoFollowRequest doFollowRequest, HttpServletRequest request) {
        ThrowUtils.throwIf(doFollowRequest == null || doFollowRequest.getFollowUserId() == null, new BusinessException(ErrorCode.PARAMS_NULL));
        //获取登录用户
        User loginUser = userService.getLoginUser(request);
        Long followUserId = doFollowRequest.getFollowUserId();
        //获取登录用户ID
        Long loginUserId = loginUser.getId();
        boolean success = followService.doFollow(loginUserId, followUserId);
        Message message = Message.builder()
                .content("关注了你")
                .userId(followUserId)
                .type(MessageType.INFO.getValue())
                .build();
        messageService.sendMessageToUser(message);
        if (Boolean.FALSE.equals(success)) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR);
        }
        return ResultUtils.success(success);

    }


    /**
     * 判断是否关注
     *
     * @param doFollowRequest 用户ID
     * @param request         HttpServletRequest对象
     * @return BaseResponse<Boolean> 返回操作结果
     */
    @PostMapping("/isFollow")
    public BaseResponse<Boolean> isFollow(@RequestBody DoFollowRequest doFollowRequest, HttpServletRequest request) {
        ThrowUtils.throwIf(doFollowRequest == null || doFollowRequest.getFollowUserId() == null, new BusinessException(ErrorCode.PARAMS_NULL));
        //获取登录用户
        User loginUser = userService.getLoginUser(request);

        Long followUserId = doFollowRequest.getFollowUserId();
        //获取登录用户ID
        Long loginUserId = loginUser.getId();
        boolean success = followService.hasFollow(loginUserId, followUserId);
        return ResultUtils.success(success);
    }

    /**
     * 获取关注用户列表
     * //r //n
     *
     * @param userId  用户ID
     * @param request HttpServletRequest对象
     * @return BaseResponse<List < UserVO>> 返回关注用户列表
     */
    @GetMapping("/followList")
    public BaseResponse<List<UserVO>> followList(@RequestParam Long userId, HttpServletRequest request) {
        ThrowUtils.throwIf(userId == null, new BusinessException(ErrorCode.PARAMS_NULL));
        userService.getLoginUser(request);
        List<Long> followIds = followService.getFollowList(userId);
        if (followIds == null) {
            return ResultUtils.success(new ArrayList<>());
        }
        List<User> followUserList = userService.listByIds(followIds);
        List<UserVO> collect = followUserList.stream().map(userService::getUserVo).collect(Collectors.toList());
        return ResultUtils.success(collect);
    }

}